<?php

namespace app\admin\controller;


use app\admin\model\FriendshipLink;
use app\admin\model\Substation;

/**
 * 友情链接管理
 * Class FriendshipLinks
 * @package app\admin\controller
 */
class FriendshipLinks extends Base
{

    //友情链接列表
    public function index(){
        if(input("?table")) {
            $keyword = input("keyword/s");
            $FriendshipLink = new FriendshipLink();
            $result["code"] = 0;
            $result['data'] = $FriendshipLink->friendshipLinkList($keyword);
            $result['count'] = $FriendshipLink->friendshipLinkListCount($keyword);
            return json($result);
        }
        return view('index',[
            'title'         =>  '友情链接列表',
        ]);
    }

    //添加修改友情链接
    public function add(){
        $link_id = input("link_id/d",0);
        $FriendshipLink = new FriendshipLink();
        if(request()->isPost()){
            $data = input("post.");
            $check =  [//验证规则
                ["link_name", "require", "链接名称不能为空"],
                ["link_url", "require|url", "链接地址不能为空|请输入有效的链接地址"],
                ["sort", "require|between:0,999", "排序不能为空|排序应该是0-999之间的值"],
                ["substation_id", "require", "请选择发布网站"],
            ];
            $msg = $FriendshipLink->checkData($data,$check);
            if ($msg === true) {
                if($FriendshipLink->checkFieldOnly("link_name",$data["link_name"],"link_id",$link_id)){
                    $result["code"] = 1;
                    $result["msg"] = '链接名称已存在';
                }else{
                    if($FriendshipLink->checkSave($link_id,$data["substation_id"])) {
                        if ($FriendshipLink->mySave($data,"link_id", $link_id)) {
                            $result["code"] = 0;
                            $result["msg"] = "操作成功！";
                        } else {
                            $result["code"] = 10000;
                            $result["msg"] = "操作失败！";
                        }
                    }else{
                        $result["code"] = 20000;
                        $result["msg"] = "没有权限操作该链接！";
                    }
                }
            }else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
            return json($result);
        }else{
            if(!empty($link_id)){
                //修改获取详情
                $dataInfo = $FriendshipLink->dataInfo("link_id",$link_id);
                $this->assign('dataInfo',$dataInfo);
            }
            $Substation = new Substation();//分站类
            $userSubstationList = $Substation->userList();//获取分站集合
            $this->assign('userSubstationList',$userSubstationList);
        }
        return view('add',[
            'title'                     =>  '添加修改友情链接',
            'RadioIs'                   =>  config("IsNoRadio"),//获取Radio配置
        ]);
    }

    //删除友情链接
    public function del(){
        $link_id = input("link_id/d",0);
        $FriendshipLink = new FriendshipLink();
        if(!empty($link_id)){
            if($FriendshipLink->checkSave($link_id,0)) {
                if ($FriendshipLink->del("link_id", $link_id)) {
                    $result["code"] = 0;
                    $result["msg"] = "操作成功!";
                } else {
                    $result["code"] = 10000;
                    $result["msg"] = "操作失败！";
                }
            }else{
                $result["code"] = 20000;
                $result["msg"] = "没有权限操作该链接！";
            }
        }else{
            $result["code"] = 1;
            $result["msg"] = "link_id,参数为空!";
        }
        return json($result);
    }
}
